/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ronsicecream.pos.mk.transaction.receiptfromwarehousedetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Logger;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.ronsicecream.pos.entities.PosTransactionOutReceiptMaterialFromWarehouseDetail;
import com.ronsicecream.pos.entities.PosTransactionWhDistributionToOutletDetail;
import java.util.List;
import javax.persistence.EntityManager;

/**
 *
 * @author user
 */
public class ReceiptDetail extends Page{
   
    
    private Field receiptDetailId;
    
    private Field receiptDetailQuantity;
    
    private Field receiptDetailPrice;
    
    private Field receiptDetailDescription;
    
    private Field receiptDetailReceiptId;
    
//    private Field receiptDetailSatuanId;
    
    private Field receiptDetailItemId;
    

    public ReceiptDetail() {
        super();

        setTheClass(PosTransactionOutReceiptMaterialFromWarehouseDetail.class);
        
        
        
        
        receiptDetailId = new Field();
        receiptDetailId.setName("receiptDetailId");
        receiptDetailId.setCaption(" id");
        //receiptDetailId.setInfo("receipt detail id");
        receiptDetailId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        receiptDetailId.setInputProperties(" size=\"20\"");        
        getFields().add(receiptDetailId);
        
        receiptDetailQuantity = new Field();
        receiptDetailQuantity.setName("receiptDetailQuantity");
        receiptDetailQuantity.setCaption(" quantity");
        receiptDetailQuantity.setRequired(true);
        receiptDetailQuantity.setListProperties(" align=\"left\" width=\"2%\" nowrap");
        receiptDetailQuantity.setInputProperties(" size=\"20\"");        
        getFields().add(receiptDetailQuantity);
        
        receiptDetailPrice = new Field();
        receiptDetailPrice.setName("receiptDetailPrice");
        receiptDetailPrice.setCaption(" price");
        receiptDetailPrice.setRequired(true);
        receiptDetailPrice.setListProperties(" align=\"left\" width=\"20%\" nowrap");
        receiptDetailPrice.setInputProperties(" size=\"20\"");        
        getFields().add(receiptDetailPrice);
        
        receiptDetailDescription = new Field();
        receiptDetailDescription.setName("receiptDetailDescription");
        receiptDetailDescription.setCaption("description");
        //receiptDetailDescription.setInfo("receipt detail description");
        receiptDetailDescription.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        receiptDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        receiptDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        receiptDetailDescription.setInputCase(Field.CASE_UPPER);        
        getFields().add(receiptDetailDescription);
        
        receiptDetailReceiptId = new Field();
        receiptDetailReceiptId.setName("receiptDetailReceiptId");
        receiptDetailReceiptId.setCaption("supplier");
        //posTransactionReceiptFromSupplier.setInfo("pos transaction receipt from supplier");
        receiptDetailReceiptId.setListProperties(" align=\"left\" width=\"20%\" nowrap");
        receiptDetailReceiptId.setInputProperties(" size=\"20\"");        
        getFields().add(receiptDetailReceiptId);
        
//        receiptDetailSatuanId = new Field();
//        receiptDetailSatuanId.setName("receiptDetailSatuanId");
//        receiptDetailSatuanId.setCaption("satuan");
//        receiptDetailSatuanId.setRequired(true);
//        receiptDetailSatuanId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
//        receiptDetailSatuanId.setInputProperties(" size=\"20\"");        
//        getFields().add(receiptDetailSatuanId);
        
        receiptDetailItemId = new Field();
        receiptDetailItemId.setName("receiptDetailItemId");
        receiptDetailItemId.setCaption("item");
        receiptDetailItemId.setRequired(true);
        receiptDetailItemId.setListProperties(" align=\"left\" width=\"30%\" nowrap");
       receiptDetailItemId.getSelectorColumns().add(new String[]{"itemName", " Nama", "width=\"40%\""});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"10%\" nowrap"});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemTypeId", " Type", "width=\"20%\""});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemMinimum", " Minimum", "width=\"10%\""});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemSatuanId", " Satuan", "width=\"10%\""});
        receiptDetailItemId.getSelectorColumns().add(new String[]{"itemRasaId", " Rasa", "width=\"10%\""}); 
//        receiptDetailItemId.setSelectorOrder("t.itemName");      
        getFields().add(receiptDetailItemId);
        

        fillFields();
        getFieldsAdd().clear();
        getFieldsAdd().add(receiptDetailItemId);
        getFieldsAdd().add(receiptDetailQuantity);
//        getFieldsAdd().add(receiptDetailSatuanId);
        getFieldsAdd().add(receiptDetailPrice);
        getFieldsAdd().add(receiptDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(receiptDetailItemId);
        getFieldsEdit().add(receiptDetailQuantity);
        getFieldsEdit().add(receiptDetailPrice);
//        getFieldsEdit().add(receiptDetailPrice);
        getFieldsEdit().add(receiptDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(receiptDetailItemId);
        getFieldsList().add(receiptDetailQuantity);
//        getFieldsList().add(receiptDetailSatuanId);
        getFieldsList().add(receiptDetailPrice);
        getFieldsList().add(receiptDetailDescription);

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);
    }
    
    @Override
    public void loadDetail(List datas, String id) {
        try {
            EntityManager em = getEntityManager();
            List<PosTransactionWhDistributionToOutletDetail> request = em.createQuery("SELECT t FROM PosTransactionWhDistributionToOutletDetail t "
                    + " WHERE t.distributionDetailDistributionId.distributionId=" + id.toString())
                    .setHint(Persistence.REFRESH_HINT, true).getResultList();
            datas.clear();
            for (PosTransactionWhDistributionToOutletDetail pd : request) {
                PosTransactionOutReceiptMaterialFromWarehouseDetail rd = new PosTransactionOutReceiptMaterialFromWarehouseDetail();
                rd.setReceiptDetailItemId(pd.getDistributionDetailItemId());
                rd.setReceiptDetailQuantity(pd.getDistributionDetailUnsent());
                System.out.println("Quantity " + rd.getReceiptDetailQuantity());
                rd.setReceiptDetailPrice(pd.getDistributionDetailPrice());
//                rd.setReceiptDetailSatuanId(pd.getPurchaseOrderDetailSatuanId());
                rd.setReceiptDetailDescription(pd.getDistributionDetailDescription());
                if ((rd.getReceiptDetailQuantity().intValue()) > 0) {
                    datas.add(rd);
                }
            }
        } catch (Exception e) {
            Logger.getInstance().log(e);
            System.out.println("Errrorr " + e);
        }
    }
}
